package com.kycxika.app.activity.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.kycxika.app.R;
import com.kycxika.app.activity.LoginActivity;
import com.kycxika.app.activity.MainActivity;
import com.kycxika.app.activity.pic.SelfInfoActivity;
import com.kycxika.app.dbutils.IntImp.KycXkIApi;
import com.kycxika.app.view.CircleImageView;
import com.zhy.android.percent.support.PercentRelativeLayout;


/**
 * Created by DELL on 2017/5/2.
 */
public class MySelfFragment extends Fragment{

    private View mView;
    private CircleImageView mFamily_image;
    private TextView family_self;
    private TextView family_name;
    private PercentRelativeLayout out_login;
    private Boolean userRegistionStatu;
    private KycXkIApi api;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = View.inflate(getActivity(), R.layout.activity_family_school, null);
        initView();
        initData();
        initListener();
        return mView;
    }

    private void initView() {
        mFamily_image = (CircleImageView) mView.findViewById(R.id.family_image);
        family_self = (TextView) mView.findViewById(R.id.family_self);   //个人信息
        family_name = (TextView) mView.findViewById(R.id.family_name);
        out_login = (PercentRelativeLayout) mView.findViewById(R.id.out_login);

        api = KycXkIApi.getInstance(getContext());
        userRegistionStatu = api.getUserRegistionStatu();  //判断是否登录

        if(userRegistionStatu) {
            out_login.setVisibility(View.VISIBLE);
            out_login.setEnabled(true);
            family_name.setText(api.getUserNick());
            if(!TextUtils.isEmpty(api.getUserHeaderUrl())) {
                Bitmap bitmap = BitmapFactory.decodeFile(api.getUserHeaderUrl());
                mFamily_image.setImageBitmap(bitmap);
            }else {
                mFamily_image.setImageResource(R.drawable.ce_01);
            }
        }else {
            out_login.setVisibility(View.INVISIBLE);
            out_login.setEnabled(false);
            family_name.setText("请登录");
            mFamily_image.setImageResource(R.drawable.ce_01);
        }
    }

    private void initData() {

    }

    private void initListener() {
        mFamily_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!userRegistionStatu) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }else {

                }
            }
        });

        mView.findViewById(R.id.system_notif).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //系统通知
                if(userRegistionStatu) {

                }else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        mView.findViewById(R.id.system_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //系统设置
                if(userRegistionStatu) {

                }else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        mView.findViewById(R.id.out_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出登录
                api.saveUerRegistionStatu(false);
                startActivity(new Intent(getActivity(), LoginActivity.class));
                MainActivity.instance.finish();
            }
        });

        family_self.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //个人设置
                if(userRegistionStatu) {
                    startActivity(new Intent(getActivity(), SelfInfoActivity.class));
                }else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
    }
}
